package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.UserBehaviorService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.article.dtos.*;
import com.heima.model.behavior.UpdateArticleMess;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author rookie001
 * @creat 2023-06-20-21:05
 */
@Service
public class UserBehaviorServiceImpl implements UserBehaviorService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private KafkaTemplate kafkaTemplate;


    //点赞
    @Override
    public ResponseResult getUserBehavior(LikesBehaviorDto dto) {
        //数据校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断点赞类型
        if (dto.getType() < 0 || dto.getType() > 3) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取用户id 判断是否登录
        Integer id = AppThreadLocalUtils.getUser().getId();
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //发消息 数据聚合
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        String key = id + dto.getArticleId() + "getUser";

        //判断是否是点赞 0为点赞 1取消点赞

        if (dto.getOperation() == 0) {
            //若为点赞 存入redis
            redisTemplate.opsForValue().set(key, dto.getOperation() + "");

            mess.setAdd(1);

        } else {
            //取消点赞 删除redis数据
            //redis中获取数据 判断
            String ope = (String) redisTemplate.opsForValue().get(key);
            if (ope != null) {
                redisTemplate.delete(key);

                mess.setAdd(-1);

            }
        }
        //发消息 .数据聚合
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //浏览次数
    @Override
    public ResponseResult readUserBehavior(ReadTimeDto dto) {
        //数据校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //获取用户id
        Integer id = AppThreadLocalUtils.getUser().getId();
        //判断用户是否登录
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //将阅读次数存入redis
        String key = id + dto.getArticleId() + "readUser";
        //查看当前浏览次数
        String snum = (String) redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(snum)) {
            redisTemplate.opsForValue().set(key, dto.getCount() + "");
        } else {
            int num = Integer.parseInt(snum);
            redisTemplate.opsForValue().set(key, (num + 1) + "");
        }

        //发送消息，数据聚合
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setAdd(1);
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    //不喜欢
    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
        //数据校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //获取用户id
        Integer id = AppThreadLocalUtils.getUser().getId();
        //判断用户是否登录
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //设置key
        String key = id + dto.getArticleId() + "unlike";
        //获取redis中数据
        String unlike = (String) redisTemplate.opsForValue().get(key);
        if (unlike == null) {
            //若为空添加
            redisTemplate.opsForValue().set(key, dto.getType() + "");
        } else {
            if (unlike.equals("0")) {
                redisTemplate.delete(key);
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
